home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / binutils / filemode.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  6KB  |  266 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "bfd.h"
  19. #include "sysdep.h"
  20.  
  21. static char ftypelet PARAMS ((unsigned long));
  22. static void setst PARAMS ((unsigned long, char *));
  23.  
  24. /* filemodestring - fill in string STR with an ls-style ASCII
  25.    representation of the st_mode field of file stats block STATP.
  26.    10 characters are stored in STR; no terminating null is added.
  27.    The characters stored in STR are:
  28.  
  29.    0    File type.  'd' for directory, 'c' for character
  30.     special, 'b' for block special, 'm' for multiplex,
  31.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  32.     '-' for any other file type
  33.  
  34.    1    'r' if the owner may read, '-' otherwise.
  35.  
  36.    2    'w' if the owner may write, '-' otherwise.
  37.  
  38.    3    'x' if the owner may execute, 's' if the file is
  39.     set-user-id, '-' otherwise.
  40.     'S' if the file is set-user-id, but the execute
  41.     bit isn't set.
  42.  
  43.    4    'r' if group members may read, '-' otherwise.
  44.  
  45.    5    'w' if group members may write, '-' otherwise.
  46.  
  47.    6    'x' if group members may execute, 's' if the file is
  48.     set-group-id, '-' otherwise.
  49.     'S' if it is set-group-id but not executable.
  50.  
  51.    7    'r' if any user may read, '-' otherwise.
  52.  
  53.    8    'w' if any user may write, '-' otherwise.
  54.  
  55.    9    'x' if any user may execute, 't' if the file is "sticky"
  56.     (will be retained in swap space after execution), '-'
  57.     otherwise.
  58.     'T' if the file is sticky but not executable. */
  59.  
  60. #if 0
  61.  
  62. /* This is not used; only mode_string is used.  */
  63.  
  64. void
  65. filemodestring (statp, str)
  66.      struct stat *statp;
  67.      char *str;
  68. {
  69.   mode_string ((unsigned long) statp->st_mode, str);
  70. }
  71.  
  72. #endif
  73.  
  74. /* Get definitions for the file permission bits.  */
  75.  
  76. #ifndef S_IRWXU
  77. #define S_IRWXU 0700
  78. #endif
  79. #ifndef S_IRUSR
  80. #define S_IRUSR 0400
  81. #endif
  82. #ifndef S_IWUSR
  83. #define S_IWUSR 0200
  84. #endif
  85. #ifndef S_IXUSR
  86. #define S_IXUSR 0100
  87. #endif
  88.  
  89. #ifndef S_IRWXG
  90. #define S_IRWXG 0070
  91. #endif
  92. #ifndef S_IRGRP
  93. #define S_IRGRP 0040
  94. #endif
  95. #ifndef S_IWGRP
  96. #define S_IWGRP 0020
  97. #endif
  98. #ifndef S_IXGRP
  99. #define S_IXGRP 0010
  100. #endif
  101.  
  102. #ifndef S_IRWXO
  103. #define S_IRWXO 0007
  104. #endif
  105. #ifndef S_IROTH
  106. #define S_IROTH 0004
  107. #endif
  108. #ifndef S_IWOTH
  109. #define S_IWOTH 0002
  110. #endif
  111. #ifndef S_IXOTH
  112. #define S_IXOTH 0001
  113. #endif
  114.  
  115. /* Like filemodestring, but only the relevant part of the `struct stat'
  116.    is given as an argument. */
  117.  
  118. void
  119. mode_string (mode, str)
  120.      unsigned long mode;
  121.      char *str;
  122. {
  123.   str[0] = ftypelet ((unsigned long) mode);
  124.   str[1] = (mode & S_IRUSR) != 0 ? 'r' : '-';
  125.   str[2] = (mode & S_IWUSR) != 0 ? 'w' : '-';
  126.   str[3] = (mode & S_IXUSR) != 0 ? 'x' : '-';
  127.   str[4] = (mode & S_IRGRP) != 0 ? 'r' : '-';
  128.   str[5] = (mode & S_IWGRP) != 0 ? 'w' : '-';
  129.   str[6] = (mode & S_IXGRP) != 0 ? 'x' : '-';
  130.   str[7] = (mode & S_IROTH) != 0 ? 'r' : '-';
  131.   str[8] = (mode & S_IWOTH) != 0 ? 'w' : '-';
  132.   str[9] = (mode & S_IXOTH) != 0 ? 'x' : '-';
  133.   setst ((unsigned long) mode, str);
  134. }
  135.  
  136. /* Return a character indicating the type of file described by
  137.    file mode BITS:
  138.    'd' for directories
  139.    'b' for block special files
  140.    'c' for character special files
  141.    'm' for multiplexor files
  142.    'l' for symbolic links
  143.    's' for sockets
  144.    'p' for fifos
  145.    '-' for any other file type. */
  146.  
  147. #ifndef S_ISDIR
  148. #ifdef S_IFDIR
  149. #define S_ISDIR(i) (((i) & S_IFMT) == S_IFDIR)
  150. #else /* ! defined (S_IFDIR) */
  151. #define S_ISDIR(i) (((i) & 0170000) == 040000)
  152. #endif /* ! defined (S_IFDIR) */
  153. #endif /* ! defined (S_ISDIR) */
  154.  
  155. #ifndef S_ISBLK
  156. #ifdef S_IFBLK
  157. #define S_ISBLK(i) (((i) & S_IFMT) == S_IFBLK)
  158. #else /* ! defined (S_IFBLK) */
  159. #define S_ISBLK(i) 0
  160. #endif /* ! defined (S_IFBLK) */
  161. #endif /* ! defined (S_ISBLK) */
  162.  
  163. #ifndef S_ISCHR
  164. #ifdef S_IFCHR
  165. #define S_ISCHR(i) (((i) & S_IFMT) == S_IFCHR)
  166. #else /* ! defined (S_IFCHR) */
  167. #define S_ISCHR(i) 0
  168. #endif /* ! defined (S_IFCHR) */
  169. #endif /* ! defined (S_ISCHR) */
  170.  
  171. #ifndef S_ISFIFO
  172. #ifdef S_IFIFO
  173. #define S_ISFIFO(i) (((i) & S_IFMT) == S_IFIFO)
  174. #else /* ! defined (S_IFIFO) */
  175. #define S_ISFIFO(i) 0
  176. #endif /* ! defined (S_IFIFO) */
  177. #endif /* ! defined (S_ISFIFO) */
  178.  
  179. #ifndef S_ISSOCK
  180. #ifdef S_IFSOCK
  181. #define S_ISSOCK(i) (((i) & S_IFMT) == S_IFSOCK)
  182. #else /* ! defined (S_IFSOCK) */
  183. #define S_ISSOCK(i) 0
  184. #endif /* ! defined (S_IFSOCK) */
  185. #endif /* ! defined (S_ISSOCK) */
  186.  
  187. #ifndef S_ISLNK
  188. #ifdef S_IFLNK
  189. #define S_ISLNK(i) (((i) & S_IFMT) == S_IFLNK)
  190. #else /* ! defined (S_IFLNK) */
  191. #define S_ISLNK(i) 0
  192. #endif /* ! defined (S_IFLNK) */
  193. #endif /* ! defined (S_ISLNK) */
  194.  
  195. static char
  196. ftypelet (bits)
  197.      unsigned long bits;
  198. {
  199.   if (S_ISDIR (bits))
  200.     return 'd';
  201.   if (S_ISLNK (bits))
  202.     return 'l';
  203.   if (S_ISBLK (bits))
  204.     return 'b';
  205.   if (S_ISCHR (bits))
  206.     return 'c';
  207.   if (S_ISSOCK (bits))
  208.     return 's';
  209.   if (S_ISFIFO (bits))
  210.     return 'p';
  211.  
  212. #ifdef S_IFMT
  213. #ifdef S_IFMPC
  214.   if ((bits & S_IFMT) == S_IFMPC
  215.       || (bits & S_IFMT) == S_IFMPB)
  216.     return 'm';
  217. #endif
  218. #ifdef S_IFNWK
  219.   if ((bits & S_IFMT) == S_IFNWK)
  220.     return 'n';
  221. #endif
  222. #endif
  223.  
  224.   return '-';
  225. }
  226.  
  227. /* Set the 's' and 't' flags in file attributes string CHARS,
  228.    according to the file mode BITS. */
  229.  
  230. static void
  231. setst (bits, chars)
  232.      unsigned long bits;
  233.      char *chars;
  234. {
  235. #ifdef S_ISUID
  236.   if (bits & S_ISUID)
  237.     {
  238.       if (chars[3] != 'x')
  239.     /* Set-uid, but not executable by owner. */
  240.     chars[3] = 'S';
  241.       else
  242.     chars[3] = 's';
  243.     }
  244. #endif
  245. #ifdef S_ISGID
  246.   if (bits & S_ISGID)
  247.     {
  248.       if (chars[6] != 'x')
  249.     /* Set-gid, but not executable by group. */
  250.     chars[6] = 'S';
  251.       else
  252.     chars[6] = 's';
  253.     }
  254. #endif
  255. #ifdef S_ISVTX
  256.   if (bits & S_ISVTX)
  257.     {
  258.       if (chars[9] != 'x')
  259.     /* Sticky, but not executable by others. */
  260.     chars[9] = 'T';
  261.       else
  262.     chars[9] = 't';
  263.     }
  264. #endif
  265. }
  266.